using System;
using System.Collections.Generic;
using System.Linq;
using Dapper;
using Dapper.Contrib.Extensions;
using Read.Models;
using Read.Utility;

namespace Read.Services
{
    public class RoleService : BaseService
    {
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <returns></returns>
        public List<RoleModel> getRoleList(QueryParamsRole query)
        {
            using (var connection = GetConnection())
            {
                var rolelist = new List<RoleModel>();
                string sql = $"SELECT * FROM `{Tables.Role}` WHERE Status = 1 AND Name LIKE @Name ORDER BY `Sort`";
                rolelist = connection.Query<RoleModel>(sql, new { Name = "%" + query.Name + "%" }).ToList();
                return rolelist;
            }
        }

        /// <summary>
        /// 角色授权
        /// </summary>
        /// <returns></returns>
        public bool authorization(int roleId, string nodes)
        {
            using (var connection = GetConnection())
            {
                //删除之前的权限，后再添加
                string sqlDel = $"DELETE FROM `{Tables.Access}` WHERE RoleId = @RoleId";
                string sqlStr = $"INSERT INTO `{Tables.Access}`(RoleId,NodeId,Level,Pid) VALUES (@RoleId,@NodeId,@Level,@Pid)";
                connection.Execute(sqlDel, new { RoleId = roleId });
                var ns = nodes.Split(',').ToList();
                var param = new List<AccessModel>();
                //Console.WriteLine("============" + ns.ToJson());
                foreach (var item in ns)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        //Console.WriteLine("--------------" + item);
                        var access = new AccessModel();
                        access.RoleId = roleId;
                        access.NodeId = item.ToInt32();
                        access.Level = 0;
                        access.Pid = 0;
                        param.Add(access);
                    }
                }
                try
                {
                    connection.Execute(sqlStr, param);
                }
                catch (Exception err)
                {
                    return false; throw err;
                }
                return true;
            }
        }
        /// <summary>
        /// 角色添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public long roleAdd(RoleModel model)
        {
            using (var connection = GetConnection())
            {
                if (string.IsNullOrEmpty(model.Name))
                {
                    return 1;
                }
                model.Pid = 1;
                model.Status = 1;
                model.CreateTime = Tools.ToUnixTime();
                model.UpdateTime = Tools.ToUnixTime();
                return connection.Insert(model);
            }
        }

        /// <summary>
        /// 根据Id查询角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RoleModel getRoleById(int id)
        {
            using (var connection = GetConnection())
            {
                var sql = $"SELECT * FROM  `{Tables.Role}` WHERE Id=@Id";
                return connection.QueryFirst<RoleModel>(sql, new { Id = id });
            }
        }

        /// <summary>
        /// 角色编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool roleEdit(RoleModel model)
        {
            using (var connection = GetConnection())
            {
                RoleModel role = getRoleById(model.Id);
                //Console.WriteLine("++++++++++++++++++" + role.ToJson());
                model.Pid = role.Pid;
                model.Status = role.Status;
                model.CreateTime = role.CreateTime;
                model.UpdateTime = Tools.ToUnixTime();
                return connection.Update(model);
            }
        }

        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool roleDel(int id)
        {
            using (var connection = GetConnection())
            {
                //RoleModel role = getRoleById(id);
                //Console.WriteLine(admin.ToJson());
                //return true;
                return connection.Delete(new RoleModel() { Id = id });
            }
        }
    }
}